package com.yanzengen.today_newstop;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;
import com.yanzengen.lazyviewpager.LazyFragmentPagerAdapter;
import com.yanzengen.today_newstop.fragment.NewFragment;

public class Today_Home_Activity extends AppCompatActivity {

    PagerSlidingTabStrip tabStrip;
    ViewPager viewPager;
    HeadAdapter headAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_today__home_);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView(){
        tabStrip=(PagerSlidingTabStrip)findViewById(R.id.tabs);
        viewPager=(ViewPager)findViewById(R.id.viewPager);
        //初始化适配器
        headAdapter = new HeadAdapter(getSupportFragmentManager());
        //将适配器绑定到viewpager
        viewPager.setAdapter(headAdapter);
        //关联viewpager和横向滚动菜单
        tabStrip.setViewPager(viewPager);

    }

    //页面适配器
    private class HeadAdapter extends LazyFragmentPagerAdapter{
        //标题集合
        private final String[] TITLES = {"推荐","热点","视频","北京","社会","娱乐","问答"
                ,"图片","科技","汽车","体育","财经","军事","国际","段子","趣图","美女","健康"};

        public HeadAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        protected Fragment getItem(ViewGroup container, int position) {
            return NewFragment.newInstance(TITLES[position],"");
        }

        /**
         *实现懒加载  要替换实现原有的getItem
         */
//        @Override
//        public Fragment getItem(int position) {
//            return NewFragment.newInstance(TITLES[position],"");
//        }

        /**
         * 获取对应标题
         * @param position
         * @return
         */
        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }
    }

}
